﻿using AutoMapper;
using Inventory.API.Application.Command.WarehouseManagementCommand;
using Inventory.API.DTO.WarehousemanagementDTO;
using Inventory.Domain;
using Inventory.Errorcode.ErrorCode;
using Inventory.Infrastructure.Interfaces;
using MediatR;

namespace Inventory.API.Application.CommandHandler.WarehousemanagementCommandHandler
{
    public class GetAllWarehousingListQueryCommandHanlder : IRequestHandler<GetAllWarehousingListQueryCommand, ApiResult<IList<WarehousemanagementDTOS>>>
    {

        private readonly ILogger<GetAllWarehousingListQueryCommandHanlder> _logger;
        private readonly IWarehousemanagementRepository repository;

        private readonly IMapper Mapper;

        public GetAllWarehousingListQueryCommandHanlder(ILogger<GetAllWarehousingListQueryCommandHanlder> logger, IWarehousemanagementRepository repository, IMapper mapper)
        {
            _logger = logger;
            this.repository = repository;
            Mapper = mapper;
        }

        public async Task<ApiResult<IList<WarehousemanagementDTOS>>> Handle(GetAllWarehousingListQueryCommand request, CancellationToken cancellationToken)
        {


            try
            {
                     var a=await repository.GetAllAsync();
                    var result = Mapper.Map<IList<WarehousemanagementDTOS>>(a);

                return ApiResult<IList<WarehousemanagementDTOS>>.Success(result, StatusCodeEnum.Success);


          
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message, "Error occured while executing GetAllWarehousingListQueryCommandHanlder");
                throw;
            }
        }
    }
}
